package redis

import (
	setting "common/viper"
	"context"
	"fmt"
	"github.com/redis/go-redis/v9"
	"log"
)

const (
	NoKey = redis.Nil
)

var (
	client *redis.Client
)

type Redis struct {
	Client *redis.Client
}

func Init(cfg *setting.RedisConfig) error {
	client = redis.NewClient(&redis.Options{
		Addr: fmt.Sprintf("%s:%d",
			cfg.Host,
			cfg.Port,
		),
		Password: cfg.Password,
		DB:       cfg.DB,
		PoolSize: cfg.PoolSize,
	})
	result, err := client.Ping(context.Background()).Result()
	if err != nil {
		log.Fatal(err)
		return err
	}
	println(result)
	return nil
}
func NewRedis() Redis {
	return Redis{
		Client: client,
	}
}

// 定义一个函数封装关闭连接
func Close() {
	_ = client.Close()
}
